<template>
	<!-- <view>1111</view> -->
	
</template>
<script>
	
	export default {
		onLoad() {
			// console.log('执行了吗')
			// uni.navigateTo({
			// 	url: '/pages/container/container'
			// })
		},
		data() {
			return {
				cityName: "Guangzhou",
				cityId: 1, //北京:1  上海:3  深圳:2
				stateSum: 0,
				stateX: 0,
				isOne: false,
				isTrue: true,
				isShow: false,
				widthItem: 0,
				city: [],
				citys: []
			}
		},
		methods: {
		inIt() {
				this.cityInit().then(res => {
					this.city.forEach(item => {
						// console.log('===>', item.id == this.cityId, item.id, this.cityId)
						if (item.id == this.cityId) {
							this.ff_click(item.id, item.bgimage);
						}
					});
				});
			},
			cityInit() {
				return new Promise((ress, rejj) => {
					this.$api.apiCities().then(res => {
						if (res.code == 200) {
							// console.log("res===>", res.data);
							this.city = res.data;
							ress();
						}
					});
				});
			},

			ff_click(id, bgimage) {
				let src = `api/cities/${id}/display`;
				// this.$store.commit("updataCityId", id);
				this.$api.apiDiplay(src).then(res => {
					if (res.code == 200) {

						// console.log("res====>", res.data);
						let newData = JSON.parse(JSON.stringify(res.data));
						newData.forEach(item => {
							item.background = bgimage;
						});
						// 
						let Ndata = this.ff_package(newData);




						// 资源本地化
						// this.getBasePack(Ndata).then(ress => {

						// });
						let Ndata_end_nunber = Ndata.length - 1;
						// Ndata = [Ndata, Ndata];
						// 
						// this.$store.commit("updataBgimage", bgimage);
						uni.setStorageSync('bgimage', bgimage);
						console.log('bgimage===>', bgimage)

						

						// console.log("数组重构后的Ndata===>", Ndata);

						// 数据资源base64化

						uni.setStorage({
							key: 'brandArrs',
							data: JSON.stringify(Ndata),
							success: function() {
								console.log('异步了!!');
								// uni.redirectTo({
								// 	url: '../container/container'
								// })
								uni.navigateTo({
									url: '../container/container'
								});
							}
						});




					}
				});
			},
			// 数据拼装
			ff_package(data) {
				// 
				let oneIcon = 0;
				let newData = [];

				//    初步数据结构
				data.forEach((item, index) => {
					let newDataItem = {};
					if (item.type == 2) {
						//  大屏
						newDataItem.type = "FULL";
						newDataItem.isTrue = true;
						newDataItem.brands = item.video.video;

					} else if (item.type == 3) {
						//  中屏
						newDataItem.type = "mediateFULL";
						newDataItem.isTrue = false;
						newDataItem.brands = item.video.video;

						newDataItem.bjSrc = [];
						let textArr = item.video.description.split("</p><p>");
						newDataItem.text01 = textArr[0].substring(3);
						newDataItem.text02 = textArr[1].substring(0, textArr[1].length - 4);
						newDataItem.bjSrc.push(item.background);
					} else if (item.type == 4) {
						console.log("item.videoitem.video====>", item.video);
						// 
						//  左屏
						newDataItem.type = "leftFULL";
						newDataItem.isTrue = false;

						newDataItem.brands = item.video.video;

						newDataItem.bjSrc = [];
						newDataItem.bjSrc.push(item.background);
						let textArr = item.video.description.split("</p><p>");
						newDataItem.text01 = textArr[0].substring(3);
						newDataItem.text02 = textArr[1].substring(0, textArr[1].length - 4);
						//    
					} else if (item.type == 1) {
						//  LOGO屏
						//    if(data[index+1] == "LOGO"){
						oneIcon++;
						newDataItem.type = "LOGO";
						newDataItem.isTrue = false;
						newDataItem.brands = [];
						newDataItem.id = `${newDataItem.type}_${index}`
						let newDataItemLi = [];
						newDataItem.bjSrc = [];
						newDataItem.bjSrc.push(item.background);

						item.logos.forEach((newItem, newIndex) => {
							let isIcon = oneIcon == 1 ? true : false;
							newDataItemLi.push({
								isIcon,
								Src: newItem.icon
							});
							// ;
							if (
								newDataItemLi.length == 3 ||
								(newDataItemLi.length != 3 && newIndex + 1 == item.logos.length)
							) {
								newDataItem.brands.push(newDataItemLi);
								newDataItemLi = [];
							}
						});

						// newDataItem.brands.push()

						//    }
					}
					newData.push(newDataItem);
				});
				let NewDatas = [];
				let isPass = true;
				//    最终数据结构
				newData.forEach((newItem, newIndex) => {
					//    
					if (newItem.type != "LOGO") {
						NewDatas.push(newItem);
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex + 1] != undefined &&
						newData[newIndex + 1].type == "LOGO" &&
						newData[newIndex + 2] != undefined &&
						newData[newIndex + 2].type == "LOGO" &&
						newData[newIndex + 3] != undefined &&
						newData[newIndex + 3].type == "LOGO" &&
						newData[newIndex + 4] != undefined &&
						newData[newIndex + 4].type == "LOGO" &&
						newData[newIndex + 5] != undefined &&
						newData[newIndex + 5].type == "LOGO"
					) {
						if (isPass) {
							isPass = false;
							let newItemss = {};
							newItemss.type = "LOGO";
							(newItemss.isTrue = false),
							(newItemss.bjSrc = [
								...newItem.bjSrc,
								...newData[newIndex + 1].bjSrc,
								...newData[newIndex + 2].bjSrc,
								...newData[newIndex + 3].bjSrc,
								...newData[newIndex + 4].bjSrc,
								...newData[newIndex + 5].bjSrc
							]);
							newItemss.brands = [
								...newItem.brands,
								[],
								...newData[newIndex + 1].brands,
								[],
								...newData[newIndex + 2].brands,
								[],
								...newData[newIndex + 3].brands,
								...newData[newIndex + 4].brands,
								[],
								...newData[newIndex + 5].brands
							];
							NewDatas.push(newItemss);
						}
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex + 1] != undefined &&
						newData[newIndex + 1].type == "LOGO" &&
						newData[newIndex + 2] != undefined &&
						newData[newIndex + 2].type == "LOGO" &&
						newData[newIndex + 3] != undefined &&
						newData[newIndex + 3].type == "LOGO" &&
						newData[newIndex + 4] != undefined &&
						newData[newIndex + 4].type == "LOGO"
					) {
						if (isPass) {
							// 
							isPass = false;
							let newItemss = {};
							newItemss.type = "LOGO";
							(newItemss.isTrue = false),
							console.log(
								"newData[newIndex+4].brands=========>",
								newData[newIndex + 4].brands
							);
							newItemss.bjSrc = [
								...newItem.bjSrc,
								...newData[newIndex + 1].bjSrc,
								...newData[newIndex + 2].bjSrc,
								...newData[newIndex + 3].bjSrc,
								...newData[newIndex + 4].bjSrc
							];
							newItemss.brands = [
								...newItem.brands,
								[],
								...newData[newIndex + 1].brands,
								[],
								...newData[newIndex + 2].brands,
								[],
								...newData[newIndex + 3].brands,
								[],
								...newData[newIndex + 4].brands
							];
							NewDatas.push(newItemss);
						}
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex + 1] != undefined &&
						newData[newIndex + 1].type == "LOGO" &&
						newData[newIndex + 2] != undefined &&
						newData[newIndex + 2].type == "LOGO" &&
						newData[newIndex + 3] != undefined &&
						newData[newIndex + 3].type == "LOGO"
					) {
						if (isPass) {
							isPass = false;
							let newItemss = {};
							newItemss.type = "LOGO";
							(newItemss.isTrue = false),
							(newItemss.bjSrc = [
								...newItem.bjSrc,
								...newData[newIndex + 1].bjSrc,
								...newData[newIndex + 2].bjSrc,
								...newData[newIndex + 3].bjSrc
							]);
							newItemss.brands = [
								...newItem.brands,
								[],
								...newData[newIndex + 1].brands,
								[],
								...newData[newIndex + 2].brands,
								[],
								...newData[newIndex + 3].brands
							];
							NewDatas.push(newItemss);
						}
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex + 1] != undefined &&
						newData[newIndex + 1].type == "LOGO" &&
						newData[newIndex + 2] != undefined &&
						newData[newIndex + 2].type == "LOGO"
					) {
						if (isPass) {
							isPass = false;
							let newItemss = {};
							newItemss.type = "LOGO";
							(newItemss.isTrue = false),
							(newItemss.bjSrc = [
								...newItem.bjSrc,
								...newData[newIndex + 1].bjSrc,
								...newData[newIndex + 2].bjSrc
							]);
							newItemss.brands = [
								...newItem.brands,
								[],
								...newData[newIndex + 1].brands,
								[],
								...newData[newIndex + 2].brands
							];
							NewDatas.push(newItemss);
						}
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex + 1] != undefined &&
						newData[newIndex + 1].type == "LOGO"
					) {
						if (isPass) {
							isPass = false;
							let newItemss = {};
							newItemss.type = "LOGO";
							(newItemss.isTrue = false),
							(newItemss.bjSrc = [
								...newItem.bjSrc,
								...newData[newIndex + 1].bjSrc
							]);
							newItemss.brands = [
								...newItem.brands,
								[],
								...newData[newIndex + 1].brands
							];
							NewDatas.push(newItemss);
						}
					} else if (
						newItem.type == "LOGO" &&
						newData[newIndex - 1].type != "LOGO" &&
						((newData[newIndex + 1] != undefined &&
								newData[newIndex + 1].type != "LOGO") ||
							newData[newIndex + 1] == undefined)
					) {
						NewDatas.push(newItem);
					}
				});
				// console.log("NewDatas=========>", NewDatas);
				// let NxutDatas = [];
				NewDatas.forEach((item, index) => {
					item.id = `${item.type}_${index}`
				});
				return NewDatas;
			}


		},
		onLoad: function() {
			let that = this;
			uni.getNetworkType({
				success: (res) => {

					let isConnected = res.networkType == "none" ? false : true;
					console.log("是否离线 true为有网 false为无网==================>", isConnected);
					if (isConnected) {
						// 直奔指定城市
						that.inIt();
					} else {
						uni.getStorage({
							key: 'brandArrs',
							success: (res) => {
								console.log('获取本地离线数据异步了!! <===========================', res);
								uni.navigateTo({
									url: '../container/container'
								});
							},
							fail: () => {
								uni.showToast({
									icon: 'error',
									title: '第一次请联网!',
									duration: 20000
								})
							}
						});
					}

				}
			});




			// uni.getNetworkType({
			//     success: function (res) {
		 //         console.log(res.networkType);
			//     },
			//  fail: function
			// });



		},

	}
</script>

<style>
	html,
	body {
		width: 100%;
		height: 100vh;

	}
   
	#app {
		width: 100%;
		height: 100vh;
	}

	/*每个页面公共css */
</style>
